package chatAI

import (
	"api/internal/svc"
	"api/internal/types"
	"context"
	"user_srv/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChatHistoryLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatHistoryLogic {
	return &ChatHistoryLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ChatHistoryLogic) ChatHistory(req *types.ChatHistoryReq) (resp *types.Response, err error) {
	history, err := l.svcCtx.UserRpc.ChatHistory(l.ctx,
		&userclient.ChatHistoryRequest{SessionId: req.SessionId})
	if err != nil {
		return &types.Response{Code: 400, Message: err.Error()}, nil
	}

	// 指针切片 → 值切片
	msgs := make([]types.Message, 0, len(history.Messages))
	for _, m := range history.Messages {
		msgs = append(msgs, types.Message{
			Role:    m.Role,
			Content: m.Content,
		})
	}

	return &types.Response{
		Code:    200,
		Message: "获取聊天记录成功",
		Data: &types.ChatHistoryResp{
			Messages: msgs,
		},
	}, nil
}
